#!/bin/bash
# SM2 Algorithm - All Intermediate Value Models Test

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BASE_DIR="$(dirname "$SCRIPT_DIR")"
SUMMARY_DIR="$BASE_DIR/summary_results"

mkdir -p "$SUMMARY_DIR"

ALGORITHM="sm2"
TRACES="${TRACES:-100}"
TRACE_DELAY="${TRACE_DELAY:-500000}"

MODELS=(
    "point_add_x"
    "point_dbl_x"
    "scalar_mult_step"
    "modular_op"
)

echo "========================================"
echo "SM2 Intermediate Value Models Testing"
echo "========================================"
echo "Traces: $TRACES"
echo "Trace Delay: ${TRACE_DELAY}us"
echo "Models: ${#MODELS[@]}"
echo ""

for model in "${MODELS[@]}"; do
    echo "----------------------------------------"
    echo "Testing: $ALGORITHM - $model"
    echo "----------------------------------------"
    
    python "$BASE_DIR/run_experiment.py" \
        -a "$ALGORITHM" \
        -m "$model" \
        -n "$TRACES" \
        -d "$TRACE_DELAY" \
        -o "${ALGORITHM}_${model}"
    
    if [ $? -eq 0 ]; then
        echo "[+] $model completed successfully"
    else
        echo "[-] $model failed"
    fi
    echo ""
done

echo "Copying results to summary directory..."
LATEST_RESULTS=$(ls -td "$BASE_DIR/results/${ALGORITHM}_"* 2>/dev/null | head -4)
if [ -n "$LATEST_RESULTS" ]; then
    for result_dir in $LATEST_RESULTS; do
        result_name=$(basename "$result_dir")
        cp "$result_dir/correlation_report.json" "$SUMMARY_DIR/${result_name}.json" 2>/dev/null || true
    done
    echo "[+] Results copied to $SUMMARY_DIR"
fi

echo ""
echo "========================================"
echo "SM2 Testing Complete"
echo "========================================"
